package com.akcome.apollo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.VMiningWorkers;
import com.akcome.apollo.dto.AccountAcceptDto;
import com.akcome.apollo.repository.VMiningWorkersRepository;
import com.akcome.apollo.service.VMiningWorkersService;


/**
 * Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class VMiningWorkersServiceImpl implements VMiningWorkersService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private VMiningWorkersRepository vMiningWorkersRepository;
	
	@Override
	public VMiningWorkers save(VMiningWorkers vMiningWorkers){
		return vMiningWorkersRepository.save(vMiningWorkers);
	}
	
	@Override
	public VMiningWorkers findOne(Long id){
		return vMiningWorkersRepository.findOne(id);
	}
	
	@Override
	public int countByPuid(Long puid,String coinType){
		return vMiningWorkersRepository.countByPuidAndCointype(puid, coinType);
	}
	
	@Override
	public int countByActive(Long puid,String coinType){
		return vMiningWorkersRepository.countByPuidAndAccept5mGreaterThanAndCointypeAndUpdatedAtGreaterThanEqual(puid, 0L, coinType,DateUtils.addHours(new Date(), -1));
	}
	
	@Override
	public List<Object> countByActive(){
		return vMiningWorkersRepository.countByActive(0L,DateUtils.addHours(new Date(), -1));
	}
	
	@Override
	public AccountAcceptDto sumAcceptBypuid(Long puid,String coinType){
		return vMiningWorkersRepository.sumAcceptBypuid(puid, coinType);
	}
	
	@Override
	public Page<VMiningWorkers> findByPage(final Long accountId,final String workerName,final String type,final String coinType,Pageable page){
		return vMiningWorkersRepository.findAll(new Specification<VMiningWorkers>() {
			
			@Override
			public Predicate toPredicate(Root<VMiningWorkers> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> params = new ArrayList<Predicate>();
				if(null!=accountId){
					params.add(criteriaBuilder.equal(root.get("puid"), accountId));
				}
				if(StringUtils.isNotBlank(coinType)){
					params.add(criteriaBuilder.equal(root.get("cointype"), coinType));
				}
				if(StringUtils.isNotBlank(workerName)){
					params.add(criteriaBuilder.like(root.get("workerName"), "%"+workerName+"%"));
				}
				if("active".equals(type)){
					//有效
					params.add(criteriaBuilder.greaterThan(root.get("accept5m"), 0));
					//最近一小时内
					params.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updatedAt"), DateUtils.addHours(new Date(), -1)));
				}else if("inactive".equals(type)){
					//无效
					params.add(criteriaBuilder.lessThanOrEqualTo(root.get("accept5m"), 0));
				}
				criteriaQuery.orderBy(criteriaBuilder.asc(root.get("workerName")));
                Predicate[] predicates = new Predicate[params.size()];
                criteriaQuery.where(params.toArray(predicates));
				
				return null;
			}
		}, page);
	}
}
